<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品列表页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../../../res/js/jquery-3.4.1.min.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">

    <div class="layui-inline">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <select name="modules" id="operator" lay-verify="required">
                <option></option>
                <option value="10">未申请</option>
                <option value="20">已申请</option>
                <option value="60">审核通过</option>
                <option value="61">审核未通过</option>
                <option value="70">正在制造</option>
                <option value="71">制造完成</option>
                <option value="80">质检中</option>
                <option value="81">已入库</option>
            </select>
        </div>
    </div>


    <div class="layui-btn-container layui-inline">

        <button class="layui-btn" lay-event="reload">搜索</button>
        <button class="layui-btn" lay-event="submitAll">批量申请</button>
        <button class="layui-btn" lay-event="addRec">增加</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    {{#if(d.state=="10"){}}
        <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
    {{#}else if(d.state=="61"){}}
        <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
    {{#}else{}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" disabled="" lay-event="del">删除</button>
    {{#}}}
    <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="watch">查看详情</button>
</script>

<script type="text/html" id="barDemo1">
    {{#if(d.state=="10"){}}
        <button class="layui-btn layui-btn-xs" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="20"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="60"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="61"){}}
        <button class="layui-btn layui-btn-xs" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="70"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs"   lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="71"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="80"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="quality">质检入库</button>
    {{#}else if(d.state=="81"){}}
        <button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="addDrug">药品操作</button>
        <button class="layui-btn layui-btn-xs layui-btn-xs layui-btn-disabled" disabled="" lay-event="made">开始制造</button>
        <button class="layui-btn layui-btn-warm layui-btn-xs layui-btn-disabled" disabled="" lay-event="quality">质检入库</button>
    {{#}}}
</script>
<script type="text/html" id="barDemo2">
    {{#if(d.state=="10"){}}
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="audit">提交申请</button>
    {{#}else if(d.state=="61"){}}
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="audit">提交申请</button>
    {{#}else{}}
        <button class="layui-btn layui-btn-normal layui-btn-xs layui-btn-disabled" disabled="" lay-event="audit">提交申请</button>
    {{#}}}
</script>

<script type="text/html" id="img">
    <img src="../user/getPicture.action?userId={{d.id}}" width="50" height="50" onclick="showBig(this)"/>
</script>

<!--<script type="text/html" id="state">
    {{#if(d.userStatu=="1"){}}
    有效
    {{#}else{}}
    无效
    {{#}}}
</script>-->


<script src="../../../res/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    var openIndex = null;
    var table = null;
    var array = new Array();
    var ids = [];
    let s = 0;
    layui.use(['table', 'layer'], function () {
        table = layui.table;

        table.render({
            elem: '#test'
            , url: '../../../hqPlan/findAll'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '生产计划单'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'planName',fixed: 'left', title: '计划名称', width: 120,align:"center"}
                , {field: 'planPrice', title: '计划金额', width: 120,align:"center"}
                , {field: 'planKtime', title: '开始日期', width: 150,align:"center"}
                , {field: 'planJtime', title: '结束日期', width: 150,align:"center"}
                , {field: 'oper', title: '操作人', width: 100,align:"center"}
                , {field: 'optime', title: '发布时间', width: 150,align:"center"}
                , {field: 'state',fixed: 'right', title: '状态', width: 100,templet:function(d){
                        if(d.state==10){
                            return '<p class=\"layui-btn layui-btn-warm layui-btn-xs\">未申请</p>'
                        }else if(d.state==20){
                            return '<p class=\"layui-btn layui-btn-warm layui-btn-xs\">已申请</p>'
                        }else if(d.state==60){
                            return '<p class="layui-btn layui-btn-warm layui-btn-xs">总公司总经理通过</p>';
                        }else if(d.state==61){
                            return '<p class="layui-btn layui-btn-danger layui-btn-xs">总公司总经理不通过</p>';
                        }else if(d.state == 70){
                            return '<p class="layui-btn layui-btn-warm layui-btn-xs">制造中</p>';
                        }else if(d.state == 71) {
                            return '<p class="layui-btn layui-btn-warm layui-btn-xs">制造完成待质检</p>';
                        }else if(d.state == 80){
                            return '<p class="layui-btn layui-btn-warm layui-btn-xs">质检中</p>'
                        }else if(d.state == 81){
                            return '<p class="layui-btn layui-btn-warm layui-btn-xs">已入库</p>'
                        }
                    },align:"center"}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 200,align:"center"}
                , {fixed: 'right', title: '关于生产', toolbar: '#barDemo1', width: 250,align:"center"}
                , {fixed: 'right', title: '提交', toolbar: '#barDemo2', width: 100,align:"center"}
            ]]
            , page: true
            , parseData: function (res, page, count) {//res 即为原始返回的数据
                ids = res.data;

                for(var i in res.data){
                    for(var j=0;j<array.length;j++){
                        if(array[j].planId==res.data[i].planId){
                            //如果set集合中有的话，给rows添加check属性选中
                            res.data[i]["LAY_CHECKED"] = true;
                        }
                    }
                }
            },
            done:function (res,curr,count) {
                if (curr > 1 && res.data.length === 0) {
                    curr = curr - 1;
                    table.reload('test', {
                        page: {
                            curr: curr
                        },
                        where: {
                        },
                    });
                }
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'reload':
                    table.reload('test',{
                        where:{
                            state: $("#operator").val()
                        }
                        ,page: {
                            curr: 1
                        }
                    })
                    break;
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'addRec':
                    //页面层
                    openIndex = layer.open({
                        type: 2,
                        skin: 'layui-layer-rim', //加上边框
                        title: '计划维护',
                        maxmin: true,
                        area: ['80%', '80%'], //宽高
                        content: 'addOrUpdate.html'
                    })
                    break;

                case 'submitAll':

                    if(array.length<1){
                        layer.msg('请选择要申请的计划单', {icon: 5, time: 2000});
                    }else{
                        let str = "";
                        for (let i in array){

                            str+=array[i].planName+' ';


                        }
                        layer.confirm('你确定要提交计划单'+JSON.stringify(str)+'?', function (index) {
                            let a = 0;
                            for(let i in array) {
                                if(array[i].state!=61 && array[i].state!=10){
                                    a++;
                                    break;
                                }
                            }
                            if(a==0){
                                submitAudit(array);
                            }else{
                                layer.msg('不能含有已提交的计划单', {icon: 5, time: 2000});
                            }
                            layer.close(index);
                        });
                    }
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;

            };
        });

        table.on('checkbox(test)', function(obj){
            var checkStatus = table.checkStatus('test');
            var data = checkStatus.data;

            if(obj.type=="all"){

                if(data.length==0){
                    console.info(ids);
                    for(var i = 0;i<ids.length;i++){
                        for(var j=0;j<array.length;j++){
                            if(ids[i].planId==array[j].planId ){
                                array.splice(j,1)
                            }
                        }

                    }
                }else{

                    $.each(data,function (index,item) {
                        if(JSON.stringify(array).indexOf(JSON.stringify(item.planId))<0){

                                array.push(item)

                        }

                    })

                }
            }else{
                if(!obj.checked){
                    for(var i=0;i<array.length;i++){
                        if(JSON.stringify(obj.data.planId)==JSON.stringify(array[i].planId)){
                            array.splice(i,1);
                            break;
                        }
                    }

                }else{
                    $.each(data,function (index,item) {
                        if(JSON.stringify(array).indexOf(JSON.stringify(item.planId))<0){

                            array.push(item)
                        }

                    })
                }
            }





        });


        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;


            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var url = "../../../hqPlan/deleteById";
                    var dataId = {planId: data.planId};
                    $.post(url, dataId, function (obb) {
                        console.info(obb)
                        layer.msg(obb.message);
                        if (obb.status == 20) {
                            obj.del();
                            table.reload('test',{
                                curr:1
                            })
                        }
                    }, "json")

                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var id = data.planId;

                openIndex = layer.open({
                    type: 2,
                    maxmin: true,
                    title: '计划单维护',
                    area: ['80%', '80%'], //宽高
                    content: 'addOrUpdate.html?id='+id,
                    /*success:function (layero,index) {
                        var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
                        var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
                        body.find("#planId").val(id);
                    }*/
                })
            }else if(obj.event === 'watch'){
                var id = data.planId;

                openIndex = layer.open({
                    type: 2,
                    maxmin: true,
                    title:'计划详情',
                    area: ['80%', '65%'], //宽高
                    content: 'pdetail.html?id='+id,
                    /*success:function (layero,index) {
                        var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
                        var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
                        body.find("#planId").val(id);
                    }*/
                })
            }else if(obj.event === 'addDrug'){
                var id = data.planId;

                openIndex = layer.open({
                    type: 2,
                    maxmin: true,
                    title: '生产计划详情',
                    area: ['100%', '100%'], //宽高
                    content: 'addDrugPlan.html?id='+id,
                    /*success:function (layero,index) {
                        var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
                        var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
                        body.find("#planId").val(id);
                    }*/
                })
            }else if(obj.event === 'audit'){
                layer.confirm('你确定要提交吗?', function (index) {
                    let rowData = new Array();
                    rowData.push(data);
                    submitAudit(rowData);
                    layer.close(index);
                });




            }else if(obj.event === 'made'){
                var id = data.planId;

                console.info(id)
                openIndex = layer.open({
                    type: 2,
                    maxmin: true,
                    title: '制造计划',
                    area: ['80%', '80%'], //宽高
                    content: '../manufacturePlan/madePdetail.html?id='+id,
                    /*success:function (layero,index) {
                        var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
                        var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
                        body.find("#planId").val(id);
                    }*/
                })
            }else if (obj.event === 'quality'){
                var id = data.planId;
                console.info(id)
                window.localStorage.setItem("planId",id);
                openIndex = layer.open({
                    type: 2,
                    title: '质检维护',
                    maxmin: true,
                    area: ['80%', '80%'], //宽高
                    content: '../manufacturePlan/quality.html',
                    /*success:function (layero,index) {
                        var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
                        var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
                        body.find("#planId").val(id);
                    }*/
                })
            }
        });
    });

    /**
     * 关闭层
     */
    function closeOpen(msg) {
        layer.close(openIndex);
        layer.msg(msg, {time: 1000}, function () {
            table.reload("test");//刷新表格
        })
    }

    function submitAudit(obd) {
        var url = "../../../hqPlan/submitAudit"
        var loa ;
        $.ajax({
            url:url,
            data:{jsonStr:JSON.stringify(obd)},
            type:'post',
            dataType:'json',
            beforeSend:function(){
                loa = layer.load(0,{shade:false,time: 4*1000});
            },
            success:function (obj) {
                if(obj.status==20){
                    layer.close(loa);
                    layer.msg(obj.message,{time: 1000}, function () {
                        array = "";
                        table.reload("test");//刷新表格
                    })
                }else {
                    s=0;
                    if(obj.data==null){
                        layer.msg(obj.message, {icon: 5, time: 2000});
                    }else{
                        layer.open({
                            type: 1
                            ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                            ,id: 'layerDemo'//防止重复弹出
                            ,content: '<div style="padding: 20px 100px;">'+ obj.data +'</div>'
                            ,btn: '确定'
                            ,title: obj.message
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                                layer.closeAll();
                            }
                        });
                        //layer.msg(obj.message+JSON.stringify(obj.data), {icon: 5, time: 2000});
                    }
                }
            }
        })
        /*$.post(url,{jsonStr:JSON.stringify(obd)},function (obj) {

        },'json')*/
    }

    /**
     * 图片
     */
    function showBig(obj) {
        $("#imgDiv").fadeToggle(1000);
        $("#imgDiv_img").prop("src", obj.src)
    }
</script>
<div id="imgDiv" style="display:none;position:absolute;left:20%;top:10%">
    <img src="" id="imgDiv_img" width="100%" height="100%" onclick="showBig(this)">
</div>
</body>
</html>